package niit.com;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("doPost" + req.getParameter("name"));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取 Cookies
        Cookie[] cookies = req.getCookies();
        for (Cookie c : cookies) {
            System.out.println("Cookie 名字：" + c.getName());
            if (c.getName().equals("name")) {
                String value = c.getValue();
            }
        }
        resp.getWriter().write("doGet" + req.getParameter("name"));

        //获取session
        HttpServlet session = (HttpServlet) req.getSession();
        System.out.println("INDEX:"+session.getID());
        session.setMaxInavtiveInterval(10);
        String name = req.getParameter("name");
        resp.getWriter().write(name);

    }
}